
package com.directthought.elasticweb.client;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * This class provides an interface to a user preferences file.
 *
 * @author <a href="mailto:dak@directThought.com">David Kavanagh</a>
 */
public class UserPrefs {
	private final static String PROP_FILE_NAME = ".ew.prefs";
	private final static String WINDOW_X_KEY = "window.pos.x";
	private final static String WINDOW_Y_KEY = "window.pos.y";
	private final static String BUCKET_KEY = "bucket";
	private final static String REFRESH_RATE_KEY = "refresh.rate";
	private final static String ACCESS_ID_KEY = "access.id";
	private final static String SECRET_KEY_KEY = "secret.key";

	private static UserPrefs singleton;
	private Properties props;
	private boolean writeThrough = true;

	private UserPrefs() {
		props = new Properties();
		try {
			props.load(new FileInputStream(System.getProperty("user.home", ".")+"/"+PROP_FILE_NAME));
		} catch (FileNotFoundException ioex) {
			// ignore.... might not be a file yet
		} catch (IOException ioex) {
			System.err.println("Error loading user preferences");
		}
	}

	public int getWindowX() {
		int pos = -1;
		try {
			pos = Integer.parseInt(props.getProperty(WINDOW_X_KEY, ""+pos));
		} catch (NumberFormatException ex) { }
		return pos;
	}

	public void setWindowX(int val) {
		props.setProperty(WINDOW_X_KEY, ""+val);
		if (writeThrough) save();
	}

	public int getWindowY() {
		int pos = -1;
		try {
			pos = Integer.parseInt(props.getProperty(WINDOW_Y_KEY, ""+pos));
		} catch (NumberFormatException ex) { }
		return pos;
	}

	public void setWindowY(int val) {
		props.setProperty(WINDOW_Y_KEY, ""+val);
		if (writeThrough) save();
	}

	public String getBucket() {
		return (String)props.getProperty(BUCKET_KEY, "");
	}

	public void setBucket(String val) {
		props.setProperty(BUCKET_KEY, val);
		if (writeThrough) save();
	}

	public long getRefreshRate() {
		long rate = 5000;
		try {
			rate = Long.parseLong(props.getProperty(REFRESH_RATE_KEY, ""+rate));
		} catch (NumberFormatException ex) { }
		return rate;
	}

	public void setRefreshRate(long val) {
		props.setProperty(REFRESH_RATE_KEY, ""+val);
		if (writeThrough) save();
	}

	public String getAccessId() {
		return (String)props.getProperty(ACCESS_ID_KEY, "");
	}

	public void setAccessId(String val) {
		props.setProperty(ACCESS_ID_KEY, val);
		if (writeThrough) save();
	}

	public String getSecretKey() {
		return (String)props.getProperty(SECRET_KEY_KEY, "");
	}

	public void setSecretKey(String val) {
		props.setProperty(SECRET_KEY_KEY, val);
		if (writeThrough) save();
	}

	public void save() {
		try {
			props.store(new FileOutputStream(System.getProperty("user.home", ".")+"/"+PROP_FILE_NAME),
						"elasticweb... a web, but elastic");
		} catch (IOException ioex) {
			System.err.println("Error saving user preferences");
		}
	}

	public static UserPrefs getInstance() {
		if (singleton == null) {
			singleton = new UserPrefs();
		}
		return singleton;
	}
}
